// logs.js
// const util = require('../../utils/util.js')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import {
  get_show,
  unbind_qinglv
} from '../../api/shool/get_score.js';

import {
  logout,
  uploadAvatar
} from '../../api/login/login_user.js';
const app = new getApp()
Page({
  data: {

    userinfo: wx.getStorageSync('userinfo'),

    show_1: '',
    list: [{
      name: '联系客服',
      img: 'https://s2.loli.net/2022/03/31/I4XtmOeZoG5QPnB.png',
      open_type: 'contact',
      tranname: 's1'
    },
    {
      name: '分享应用',
      img: 'https://s2.loli.net/2022/03/31/kJ6QsiT7P2pyRcA.png',
      open_type: 'share',
      tranname: 's3'

    },

    {
      name: '意见反馈',
      img: 'https://s2.loli.net/2022/03/31/nFPV7JRrBOgG9Xu.png',
      tranname: 's2',


    },
    {
      name: '设置头像',
      img: 'https://s2.loli.net/2022/03/31/OeHImpt4EgZafiv.png',
      // img:'https://sm.ms/image/pbAh8v9KVSUfQL7',
      open_type: 'chooseAvatar',
      tranname: 's4'
    }
      // {
      //   name: '关于我们',
      //   img: 'https://s2.loli.net/2022/03/31/OeHImpt4EgZafiv.png',
      //   // img:'https://sm.ms/image/pbAh8v9KVSUfQL7',
      //   // open_type: 'chooseAvatar',
      //   tranname: 's4'
      // }

    ]
  },
  //关于我们
  gy() {
    wx.navigateTo({
      url: '../about/about',
    })
  },

  

  //微信云存储
  uploadFilePromise(chooseResult) {
    let that = this
    wx.uploadFile({
      url: 'https://d.flya.top:31193/comment/uploadLeaveImgs', //仅为示例，非真实的接口地址
      filePath: chooseResult,
      name: 'files',
      header: {
        'content-type': 'multipart/form-data',
        Authorization: wx.getStorageSync('token')
      },

      success(res) {
        const data = JSON.parse(res.data)
        //do something


        if (data.statusCode == 200) {
          that.data.userinfo.avatar = data.data[0]
          that.setData({
            img: that.data.userinfo.avatar
          })

          wx.setStorageSync('userinfo', that.data.userinfo)
          uploadAvatar(
            that.data.userinfo.avatar
          ).then(res => {

          })


          Notify({
            type: 'success',
            message: '设置头像成功'
          });

        } else {


        }


      },


    })
  },

  get_vatar(e) {
    if (this.data.userinfo) {
      this.uploadFilePromise(e.detail.avatarUrl)
    } else {
      wx.showToast({
        title: '你还未登录',
        icon: 'none',
        duration: 1000
      })
    }


  },

  bangding() {

    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  lout() {
    Dialog.confirm({
      title: '解除绑定',
      message: '您确定要解除账号的绑定吗？',
    })
      .then(() => {
        logout({
          token: wx.getStorageSync('token')
        })

        wx.removeStorageSync('userinfo'),
          wx.removeStorageSync('token'),
          wx.removeStorageSync('dayke_list'),
          wx.removeStorageSync('ke_list'),
          wx.removeStorageSync('kebiao_list'),
          wx.removeStorageSync('openid'),
          wx.removeStorageSync('wx_user_info'),
          wx.removeStorageSync('user'),
          wx.removeStorageSync('announcement'),
          wx.removeStorageSync('ke_time'),
          wx.removeStorageSync('ke_time_love'),
          wx.removeStorageSync('dayke_list_love'),
          wx.removeStorageSync('ke_list_love'),

          // wx.removeStorageSync('frist_login'),

          wx.navigateTo({
            url: '/pages/login/login',
          })

      })
      .catch(() => {
        // on cancel
      });


  },
  redmove(e) {
 
    if ( e.detail.y==136.5||e.detail.y==0) {
      //节流
      wx.vibrateShort({
        type: 'heavy',
        success: res => {
          console.log(res)
        }
      })
    }
    this.setData({
      sty: {
        w: e.detail.y,
        h: e.detail.y
      }
    })
  },


  onLoad: function () {

    this.app = getApp()


  },
  //页面初次渲染完成
  onReady: function () {

  },
  //解除绑定
  lout_love() {
    console.log("解除绑定")
    //提示用户是否确定解除绑定
    Dialog.confirm({
      title: '解除绑定',
      message: '您确定要解除情侣课表的绑定吗？',
    })
      .then(() => {
        unbind_qinglv().then(res => {
          if (res.statusCode == 200) {
            //提示用户解除绑定成功
            wx.showToast({
              title: '解绑成功',
              icon: 'none',
              duration: 1000
            })
            //清除缓存
            wx.removeStorageSync('ke_list_love'),
              wx.removeStorageSync('dayke_list_love'),
              wx.removeStorageSync('ke_time_love')

            //修改缓存user用户信息
            let user = wx.getStorageSync('user')
            user.birthday = '',
              //存入缓存
              wx.setStorageSync('user', user)

            this.setData({
              is_love: false
            })

          }

        })
      })
  },

  async onShow() {
    // this.data.userinfo=wx.getStorageSync('userinfo')




    await get_show().then(res => {
      if (res.data.password == app.getVersion()) {
        this.data.show_1 = res.data.stuNumber
     
      }else{
        this.data.show_1 = 'false'
      }

      if (this.data.show_1 == 'true') {
        this.data.list[2].open_type = "feedback"
        this.setData({
          list: this.data.list
        })
      }
    })
    this.data.userinfo = wx.getStorageSync('userinfo')
    if (wx.getStorageSync('userinfo')) {
      if (this.data.userinfo.tea_unit.value.length > 8) {
        this.data.userinfo.tea_unit.value = this.data.userinfo.tea_unit.value.slice(0, 8) + '...'
      }

    }
    // if(wx.getStorageSync('user').birthday){

    //在数组指定位置插入
    // this.data.list.splice(1, 0, {
    //   name: '解除情侣绑定',
    //   img: 'https://s2.loli.net/2022/03/31/OeHImpt4EgZafiv.png',
    //   open_type: 'lout_love',
    //   tranname: 's4'
    // })
    // this.setData({
    //   list: this.data.list
    // })



    this.setData({
      is_love: wx.getStorageSync('user').birthday ? true : false,
      show_1: this.data.show_1,
      userinfo: this.data.userinfo,
      img: this.data.userinfo.avatar,

    })
  },
  onShareAppMessage(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮

    }

    return {
      title: '校园服务',
      path: '/pages/index/index',
      imageUrl: 'https://s2.loli.net/2022/05/02/sAeh9zdt3VuIxOo.png'
    }
  },


})